import Filter from '@src/filter/filter.js';
import RenderCell from 'packages/BaseTableCell/renderCell.js';
import formatColumn from 'packages/BaseTableCell/formatColumn';
import { isUndefined } from '@src/util/type'
import { isCalendar } from '@src/util/CalendarUtil';
import { Inquire } from '../../list/TaskFieldModel'
import i18n from '@src/locales'
import { 
  isNotSupportedDisplayField, 
  isSystemFiled, 
} from '@service/FieldService'

const inquireMap = {}
Inquire.forEach(field => {
  field.show = true; 
  inquireMap[field.fieldName] = field; 
})

// 高级搜索
export const getAdvancedFields = (vm) => {
  const that = vm
  const customerField = that.customerField || {}
  const customerOption = customerField?.setting?.customerOption || {}
  const systemFieldSetting = that.systemFieldSetting || {}
  // const isCalendar = JSON.parse(localStorage.getItem('isCalendar'))
  return [
    {
      displayName: i18n.t('common.task.taskType'),
      fieldName: 'taskType',
      formType: 'select',
      setting: {
        isMulti: false,
        dataSource: that.taskTypeList
      },
    },
    {
      fieldName: 'customer',
      displayName: i18n.t('common.base.customer'),
      formType: 'text',
    },
    {
      fieldName: 'tlmName',
      displayName: i18n.t('common.base.contact'),
      formType: 'select',
      show: customerOption.linkman
    },
    {
      fieldName: 'area',
      exportAlias: 'customerAddress',
      displayName: i18n.t('task.list.displayName.area'),
      formType: 'address',
      show: customerOption.address
    },
    {
      fieldName: 'cusAddress',
      displayName: i18n.t('task.list.displayName.detailAddress'),
      formType: 'text',
      show: customerOption.address
    },
    {
      fieldName: 'product',
      displayName: i18n.t('common.base.product'),
      formType: 'text',
      show: customerOption.product
    },
    {
      fieldName: 'serviceType',
      displayName: i18n.t('common.form.type.serviceType'),
      formType: 'select',
      show: systemFieldSetting.hasServiceType,
      setting: {
        dataSource: that.serviceTypeField?.setting?.dataSource || []
      }
    },
    {
      fieldName: 'serviceContent',
      displayName: i18n.t('common.form.type.serviceContent'),
      formType: 'select',
      show: systemFieldSetting.hasServiceContent,
      setting: {
        dataSource: that.serviceContentField?.setting?.dataSource || []
      }
    },
    {
      fieldName: 'level',
      displayName: i18n.t('common.form.type.level'),
      formType: 'select',
      show: systemFieldSetting.hasLevel,
      setting: {
        dataSource: that.levelField?.setting?.dataSource || []
      }
    },
    {
      fieldName: 'description',
      displayName: i18n.t('common.form.type.description'),
      formType: 'description',
      show: systemFieldSetting.hasDescription
    },
    {
      fieldName: 'paymentMethod',
      displayName: i18n.t('task.detail.components.payMethod'),
      formType: 'select',
      show: that.paymentConfig?.version == 1,
      setting: {
        isMulti: false, 
        dataSource: [
          {
            text: i18n.t('task.detail.components.onlinePayByAli'),
            value: '在线支付-支付宝'
          },
          {
            text: i18n.t('task.detail.components.aliPayCode'),
            value: '支付宝收款码'
          },
          {
            text: i18n.t('task.detail.components.weChatCode'),
            value: '微信收款码'
          },
          {
            text: i18n.t('task.detail.components.bankCardCollection'),
            value: '银行卡收款'
          },
          {
            text: i18n.t('task.detail.components.otherPayUnifiedCollection'),
            value: '其他支付-统一收款'
          },
          {
            text: i18n.t('task.detail.components.otherPayMonthCollection'),
            value: '其他支付-月结'
          },
          {
            text: i18n.t('task.detail.components.otherPayWeekCollection'),
            value: '其他支付-周结'
          },
        ]
      }
    },
    {
      fieldName: 'tags',
      displayName: i18n.t('task.list.displayName.chooseTag'),
      formType: 'select',
    },
    {
      fieldName: 'createUser',
      displayName: i18n.t('common.base.createUser'),
      formType: 'user',
      defineType: []
    },
    {
      fieldName: 'executor',
      displayName: i18n.t('task.executor'),
      formType: 'user',
      defineType: []
    },
    {
      fieldName: 'synergyId',
      displayName: i18n.t('task.synergies'),
      formType: 'user',
      defineType: []
    },
    {
      isSystem: 1,
      fieldName: 'createTime',
      displayName: i18n.t('common.base.createTime'),
      formType: 'datetime',
      orderId: 1,
    },
    {
      fieldName: 'allotTime',
      displayName: i18n.t('task.list.displayName.allotTime'),
      formType: 'datetime',
    },
    {
      fieldName: 'acceptTime',
      displayName: i18n.t('task.list.displayName.acceptTime'),
      formType: 'datetime',
    },
    {
      fieldName: 'startTime',
      displayName: i18n.t('task.list.displayName.startTime'),
      formType: 'datetime',
    },
    {
      fieldName: 'reallotTime',
      displayName: i18n.t('task.list.displayName.transferTime'),
      formType: 'datetime',
    },
    {
      fieldName: 'planTime',
      displayName: i18n.t('common.form.type.planTime'),
      formType: 'datetime',
      show: !isCalendar
    },
    {
      fieldName: 'planStartTime',
      displayName: i18n.t('common.form.type.planStartTime'),
      formType: 'datetime',
      show: isCalendar
    },
    {
      fieldName: 'planEndTime',
      displayName: i18n.t('common.form.type.planEndTime'),
      formType: 'datetime',
      show: isCalendar
    },
    {
      fieldName: 'completeTime',
      displayName: i18n.t('common.base.completeTime'),
      formType: 'datetime',
    },
    {
      fieldName: 'reviewTime',
      displayName: i18n.t('task.detail.components.reviewTime'),
      formType: 'datetime',
    },
    {
      fieldName: 'balanceTime',
      displayName: i18n.t('task.list.displayName.balanceTime'),
      formType: 'datetime',
    },
    {
      fieldName: 'closeTime',
      displayName: i18n.t('task.list.displayName.closeTime'),
      formType: 'datetime',
    },
    {
      fieldName: 'updateTime',
      displayName: i18n.t('task.list.displayName.updateTime'),
      formType: 'datetime',
    },
    inquireMap.allotTypeStr,
    inquireMap.onceException,
    inquireMap.onceReallot,
    inquireMap.oncePrinted,
    inquireMap.inApprove,
  ].map(item => {
    return {
      ...item,
      setting: item.setting || {},
      defaultValue: null,
      show: isUndefined(item.show) ? true : item.show,
      isSearch: 1,
      isNull: 1,
      isSystem: 1,
      orderId: 1
    }
  });
}


// 列表 && 选择列
export const getColumnFields = (vm) => {
  const that = vm;
  const customerField = that.customerField || {}
  const customerOption = customerField?.setting?.customerOption || {}
  // const isCalendar = JSON.parse(localStorage.getItem('isCalendar'))

  const fields = [
    {
      fieldName: 'taskNo',
      displayName: i18n.t('common.form.type.taskNo'),
      renderCell: (h, col, row) => {
        return RenderCell.renderTask.call(that, h, row, that.allowTaskViewDetail);
      }
    },
    {
      displayName: i18n.t('common.task.taskType'),
      fieldName: 'taskType',
      renderCell: (h, col, row) => {
        return (<div>{row.templateName || ''}</div>)
      }
    },
    {
      fieldName: 'customer',
      displayName: i18n.t('common.base.customer'),
      renderCell: (h, col, row) => {
        let displayName = row['customerEntity'] && row['customerEntity'].name;
        let customerId = row?.customerEntity?.id;
        return RenderCell.renderCustomer.call(that, h, displayName, customerId, true)
      }
    },
    {
      fieldName: 'tlmName',
      displayName: i18n.t('common.base.contact'),
      show: customerOption.linkman,
      visibleForSelectColumn: customerOption.linkman,
      renderCell: (h, col, row) => {
        return (<div>
          { row['linkMan'] && row['linkMan'].name }
        </div>)
      }
    },
    {
      fieldName: 'tlmPhone',
      displayName: i18n.t('common.base.phone'),
      show: customerOption.linkman,
      visibleForSelectColumn: customerOption.linkman,
      renderCell: (h, col, row) => {
        return (<div>
          { row['linkMan'] && row['linkMan'].phone }
        </div>)
      }
    },
    {
      fieldName: 'area',
      displayName: i18n.t('common.form.type.customerAddress'),
      show: customerOption.address,
      visibleForSelectColumn: customerOption.address,
      renderCell: (h, col, row) => {
        return (<div>{Filter.prettyAddress(row.address)}</div>)
      }
    },
    {
      fieldName: 'product',
      displayName: i18n.t('common.base.product'),
      show: customerOption.product,
      visibleForSelectColumn: customerOption.product,
      renderCell: (h, col, row) => {
        return (<div>
          {row.products
            && row.products.map((product) => product.name).join(', ')
          }
        </div>);
      }
    },
    {
      fieldName: 'serviceType',
      displayName: i18n.t('common.form.type.serviceType'),
      show: that.sysFieldsSetting?.hasServiceType,
    },
    {
      fieldName: 'serviceContent',
      displayName: i18n.t('common.form.type.serviceContent'),
      show: that.sysFieldsSetting?.hasServiceContent,
    },
    {
      fieldName: 'paymentMethod',
      displayName: i18n.t('task.detail.components.payMethod'),
      show: that.paymentConfig?.version === 1,
      renderCell: (h, col, row) => {
        return (row.attribute && row.attribute.paymentMethod)
      }
    },
    {
      fieldName: 'executorName',
      displayName: i18n.t('task.executor')
    },
    {
      fieldName: 'synergies',
      displayName: i18n.t('task.synergies'),
    },
    {
      fieldName: 'completeTime',
      displayName: i18n.t('common.base.completeTime'),
    },
    {
      fieldName: 'reviewTime',
      displayName: i18n.t('task.detail.components.reviewTime'),
    },
    {
      fieldName: 'balanceTime',
      displayName: i18n.t('task.list.displayName.balanceTime'),
    },
    {
      fieldName: 'closeTime',
      displayName: i18n.t('task.list.displayName.closeTime'),
    },
    {
      fieldName: 'description',
      displayName: i18n.t('common.form.type.description'),
      visable: that.sysFieldsSetting?.hasDescription,
    },
    {
      fieldName: 'createUserName',
      displayName: i18n.t('common.base.createUser')
    },
    {
      fieldName: 'createTime',
      displayName: i18n.t('common.base.createTime'),
    },
    {
      fieldName: 'allotTime',
      displayName: i18n.t('task.list.displayName.allotTime'),
    },
    {
      fieldName: 'acceptTime',
      displayName: i18n.t('task.list.displayName.acceptTime'),
    },
    {
      fieldName: 'startTime',
      displayName: i18n.t('task.list.displayName.startTime'),
    },
    {
      fieldName: 'reallotTime',
      displayName: i18n.t('task.list.displayName.transferTime'),
      formType: 'datetime',
    },
    {
      fieldName: 'level',
      displayName: i18n.t('common.form.type.level'),
      visable: that.sysFieldsSetting?.hasLevel,
    },
    {
      fieldName: 'planTime',
      displayName: i18n.t('common.form.type.planTime'),
      show: !isCalendar,
      visibleForSelectColumn: !isCalendar,
    },
    {
      fieldName: 'planStartTime',
      displayName: i18n.t('common.form.type.planStartTime'),
      show: isCalendar,
      visibleForSelectColumn: isCalendar
    },
    {
      fieldName: 'planEndTime',
      displayName: i18n.t('common.form.type.planEndTime'),
      show: isCalendar,
      visibleForSelectColumn: isCalendar
    },
    {
      fieldName: 'degree',
      displayName: i18n.t('common.base.degree'),
    },
    {
      fieldName: 'allotTypeStr',
      displayName: i18n.t('task.list.displayName.allotType'),
      renderCell: (h, col, row) => {
        let allotTypeText = {
          1: i18n.t('task.detail.components.manualDispatch'),
          2: i18n.t('task.detail.components.poolDispatch'),
          3: i18n.t('task.detail.components.autoDispatch')
        };
        return (<div>{allotTypeText[row.allotType] || ''}</div>);
      }
    },
    {
      fieldName: 'acceptUsedTime',
      displayName: i18n.t('task.detail.components.acceptTime'),
      renderCell: (h, col, row) => {
        return RenderCell.renderTime(h, row[col.field]);
      }
    },
    {
      fieldName: 'taskUsedTime',
      displayName: i18n.t('task.detail.components.taskTime'),
      renderCell: (h, col, row) => {
        return RenderCell.renderTime(h, row[col.field]);
      }
    },
    {
      fieldName: 'workUsedTime',
      displayName: i18n.t('task.detail.components.workTime'),
      renderCell: (h, col, row) => {
        return RenderCell.renderTime(h, row[col.field]);
      }
    },
    {
      fieldName: 'taskResponseTime',
      displayName: i18n.t('task.detail.components.respondTime'),
      renderCell: (h, col, row) => {
        return RenderCell.renderTime(h, row[col.field]);
      }
    },
    {
      fieldName: 'onceOverTime',
      displayName: i18n.t('common.task.exceptionStatus.onceOverTime'),
      renderCell: (h, col, row) => {
        return RenderCell.renderBoolean(h, row[col.field]);
      }
    },
    {
      fieldName: 'onceRefused',
      displayName: i18n.t('common.task.exceptionStatus.onceRefused'),
      renderCell: (h, col, row) => {
        return RenderCell.renderBoolean(h, row[col.field]);
      }
    },
    {
      fieldName: 'oncePaused',
      displayName: i18n.t('common.task.exceptionStatus.oncePaused'),
      renderCell: (h, col, row) => {
        return RenderCell.renderBoolean(h, row[col.field]);
      }
    },
    {
      fieldName: 'onceRollback',
      displayName: i18n.t('common.task.exceptionStatus.onceRollback'),
      renderCell: (h, col, row) => {
        return RenderCell.renderBoolean(h, row[col.field]);
      }
    },
    {
      fieldName: 'onceReallot',
      displayName: i18n.t('common.task.exceptionStatus.onceAllot'),
      renderCell: (h, col, row) => {
        return RenderCell.renderBoolean(h, row[col.field]);
      }
    },
    {
      fieldName: 'oncePrinted',
      displayName: i18n.t('task.list.displayName.oncePrinted'),
      renderCell: (h, col, row) => {
        return RenderCell.renderBoolean(h, row[col.field]);
      }
    },
    {
      fieldName: 'positionException',
      displayName: i18n.t('common.task.exceptionStatus.positionException'),
      renderCell: (h, col, row) => {
        return RenderCell.renderBoolean(h, row[col.field]);
      }
    },
    {
      fieldName: 'updateTime',
      displayName: i18n.t('task.list.displayName.lastUpdated'),
    },
    {
      fieldName: 'closeOperation',
      displayName: i18n.t('common.base.operation'),
      formType: 'text',
      fixed: 'right',
      width: '88px',
      show: 'important'
    }
  ]

  return fields.map(item => {
    // 初始化属性
    let field = {
      ...item,
      setting: item.setting || {},
      isSystem: 1,
      isNull: 1,
      isSearch: 1,
      orderId: 1,
      show: isUndefined(item.show) ? true : item.show,
      visibleForSelectColumn: isUndefined(item.visibleForSelectColumn) ? true : item.visibleForSelectColumn,
    };
    return formatColumn(field); 
  })
};
